using System;
using System.Collections.Generic;
using System.Text;
using Umdf.ComInterop;
using Umdf.ComInterop;
using System.Runtime.InteropServices;

namespace Umdf.SideShow
{
    [ComVisible(true)]
    [Guid("B4C2C8D0-1BFB-41e7-8328-2C68DD9F212E")]
    public class SideShowQueue<T, R> : 
        IQueueCallbackDeviceIoControl, 
        IQueueCallbackCreate, 
        IQueueCallbackRead, 
        IQueueCallbackWrite, 
        IFileCallbackClose, 
        IFileCallbackCleanup where T : SideShowDevice<R> where R : SideShowDriver, new()
    {
        private T _parentDevice;

        public SideShowQueue(T parentDevice)
        {
            _parentDevice = parentDevice;
        }

        #region IQueueCallbackDeviceIoControl Members

        public void OnDeviceIoControl(IWDFIoQueue pWdfQueue, IWDFIoRequest pWdfRequest, uint ControlCode, uint InputBufferSizeInBytes, uint OutputBufferSizeInBytes)
        {
            uint written = 0;
            int hr = 0;
            if (NativeMethods.IsWpdIoCtl(ControlCode))
            {
                try
                {
                    _parentDevice.ProcessIoControl(pWdfQueue, pWdfRequest, ControlCode, InputBufferSizeInBytes, OutputBufferSizeInBytes, out written);
                }
                catch (COMException ex)
                {
                    hr = ex.ErrorCode;
                }
                pWdfRequest.CompleteWithInformation(hr, written);
            }
            else
            {
                hr = ComHResults.HResultFromWin32(NativeMethods.ERROR_NOT_SUPPORTED);
                pWdfRequest.CompleteWithInformation(hr, written);
            }
        }

        #endregion

        #region IQueueCallbackCreate Members

        public void OnCreateFile(IWDFIoQueue pWdfQueue, IWDFIoRequest pWdfRequest, IWDFFile pWdfFileObject)
        {
            pWdfRequest.Complete(ComHResults.S_OK);
        }

        #endregion

        #region IQueueCallbackRead Members

        public void OnRead(IWDFIoQueue pWdfQueue, IWDFIoRequest pWdfRequest, uint NumOfBytesToRead)
        {
            int hr = ComHResults.HResultFromWin32(NativeMethods.ERROR_NOT_SUPPORTED);
            pWdfRequest.CompleteWithInformation(hr, 0);
        }

        #endregion

        #region IQueueCallbackWrite Members

        public void OnWrite(IWDFIoQueue pWdfQueue, IWDFIoRequest pWdfRequest, uint NumOfBytesToWrite)
        {
            int hr = ComHResults.HResultFromWin32(NativeMethods.ERROR_NOT_SUPPORTED);
            pWdfRequest.CompleteWithInformation(hr, 0);
        }

        #endregion

        #region IFileCallbackClose Members

        public void OnCloseFile(IWDFFile pWdfFileObject)
        {
            
        }

        #endregion

        #region IFileCallbackCleanup Members

        public void OnCleanupFile(IWDFFile pWdfFileObject)
        {
            
        }

        #endregion
    }
}
